package com.zx.mes.upms.hyl.entity;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.*;


/**
 * 
 * 
 * @author hyl
 * @email 632105841@qq.com
 * @date 2018-04-08 15:10:44
 */
@Table(name = "user")
public class User implements UserDetails {
	    //
    @Id
    private String id;
	
	    //
    @Column(name = "username")
    private String username;
	
	    //
    @Column(name = "password")
    private String password;
	
	    //
    @Column(name = "createdatetime")
    private Date createdatetime;
	
	    //
    @Column(name = "modifydatetime")
    private Date modifydatetime;

    @Transient
	private List<Role> roleList = new ArrayList<>(10);

    @Transient
	private List<Menu> menuList = new ArrayList<>(40);

    @Transient
	private List<Tresource> resourceList = new ArrayList<>(50);

	/**
	 * 用户有权访问的所有url，不持久化到数据库
	 */
	@Transient
	private Set<String> urls = new HashSet<>();

	/**
	 * 设置：
	 */
	public void setId(String id) {
		this.id = id;
	}
	/**
	 * 获取：
	 */
	public String getId() {
		return id;
	}
	/**
	 * 设置：
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	/**
	 * 获取：
	 */
	@Override
	public String getUsername() {
		return username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	/**
	 * 设置：
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return null;
	}

	/**
	 * 获取：
	 */
	@Override
	public String getPassword() {
		return password;
	}
	/**
	 * 设置：
	 */
	public void setCreatedatetime(Date createdatetime) {
		this.createdatetime = createdatetime;
	}
	/**
	 * 获取：
	 */
	public Date getCreatedatetime() {
		return createdatetime;
	}
	/**
	 * 设置：
	 */
	public void setModifydatetime(Date modifydatetime) {
		this.modifydatetime = modifydatetime;
	}
	/**
	 * 获取：
	 */
	public Date getModifydatetime() {
		return modifydatetime;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public List<Menu> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<Menu> menuList) {
		this.menuList = menuList;
	}

	public List<Tresource> getResourceList() {
		return resourceList;
	}

	public void setResourceList(List<Tresource> resourceList) {
		this.resourceList = resourceList;
	}


	/**
	 * @return the urls
	 */
	public Set<String> getUrls() {
		init(urls);

		for (Role role:roleList){
			for (int i = 0; i <role.getResourceList().size() ; i++) {
				urls.add(role.getResourceList().get(i).getUrl());
				resourceList.add(role.getResourceList().get(i));
			}
		}
		return urls;
	}

	/**
	 * @param data
	 * @param
	 */
	private void init(Set<?> data){
		if (CollectionUtils.isEmpty(data)) {
			if (data == null) {
				data = new HashSet<>();
			}
		}
	}


}
